﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using Crownwood.Magic.Common;
using Crownwood.Magic.Docking;


namespace Codematic
{
    public partial class MainForm : Form
    {
        #region 参数初始化配置
        Thread threadUpdate;
        public Mutex mutex;
        public static LTP.CmConfig.ModuleSettings setting = new LTP.CmConfig.ModuleSettings();

        LTP.CmConfig.AppSettings appsettings;
        string cmcfgfile = Application.StartupPath + @"\cmcfg.ini";
        LTP.Utility.INIFile cfgfile;
        FrmSearch frmSearch;  //查找 

        private object[] persistedSearchItems;
        delegate void SetStatusCallback(string text);
        delegate void AddNewTabPageCallback(Control control, string Title);


        private DockingManager dockManager;
        private DockingManager DBdockManager;

        Content DbViewContent;
        Content tempViewContent;


        #endregion
        public MainForm()
        {
            InitializeComponent();
            mutex = new Mutex(false, "SINGLE_INSTANCE_MUTEX");
            if (!mutex.WaitOne(0, false))
            {
                mutex.Close();
                mutex = null;
            }
            this.Text = "动软.NET代码生成器  V" + Application.ProductVersion;

            webBrowser1.Url = new System.Uri("http://www.maticsoft.com/codematic/count.htm?v=" + Application.ProductVersion, System.UriKind.Absolute);

            #region 左侧视图

            DBdockManager = new DockingManager(this, VisualStyle.IDE);

            //定义对象OuterControl，Docking Manager不会关注该对象以后生成的对象的窗口区域
            //对象InnerControl，Docking Manager不会关注在该对象生成以前的对象的窗口区域
            DBdockManager.OuterControl = statusBar;
            DBdockManager.InnerControl = tabControlMain;


            //数据库视图
            #region 数据库视图管理
            DbViewContent = new Content(DBdockManager);
            DbViewContent.Control = new DbView(this);
            Size DbViewSize = DbViewContent.Control.Size;

            DbViewContent.Title = "数据库视图";
            DbViewContent.FullTitle = "数据库视图";
            DbViewContent.AutoHideSize = DbViewSize;
            DbViewContent.DisplaySize = DbViewSize;
            DbViewContent.ImageList = leftViewImgs;
            DbViewContent.ImageIndex = 0;
            #endregion


            #region 模版视图
            //模版视图 待添加
            tempViewContent = new Content(DBdockManager);
            tempViewContent.Control = new TempView();
            Size tempViewSize = tempViewContent.Control.Size;

            tempViewContent.Title = "模版管理";
            tempViewContent.FullTitle = "模版管理";
            tempViewContent.AutoHideSize = tempViewSize;
            tempViewContent.DisplaySize = tempViewSize;
            tempViewContent.ImageList = leftViewImgs;
            tempViewContent.ImageIndex = 1;
            #endregion

            //将浮动窗口和具体在浮动窗口中被包含的面板联系起来

            DBdockManager.Contents.Add(DbViewContent);
            DBdockManager.Contents.Add(tempViewContent);

            WindowContent wcdb = DBdockManager.AddContentWithState(DbViewContent, State.DockLeft);
            DBdockManager.AddContentToWindowContent(tempViewContent, wcdb);

            #endregion

        }

        private void 保存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int c = 1;
            c = int.Parse("a");
        }

        private void 项目PToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 文件FToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void 数据库连接SToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 服务器资源管理器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Content content = DBdockManager.Contents["数据库视图"];
            if (服务器资源管理器SToolStripMenuItem.Checked)
            {
                DBdockManager.HideContent(content);
                服务器资源管理器SToolStripMenuItem.Checked = false;
            }
            else
            {
                DBdockManager.ShowContent(content);
                服务器资源管理器SToolStripMenuItem.Checked = true;
            }
        }

        private void 模板管理器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Content content = DBdockManager.Contents["模版管理"];
            if (模板管理器ToolStripMenuItem.Checked)
            {
                DBdockManager.HideContent(content);
                模板管理器ToolStripMenuItem.Checked = false;
            }
            else
            {
                DBdockManager.ShowContent(content);
                模板管理器ToolStripMenuItem.Checked = true;
            }


        }

        private void 数据库摘要ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddSinglePage(new DbBrowser(), "摘要");
        }

        public void AddNewTabPage(Control control, string Title)
        {
            if (this.tabControlMain.InvokeRequired)
            {
                AddNewTabPageCallback d = new AddNewTabPageCallback(AddNewTabPage);
                this.Invoke(d, new object[] { control, Title });
            }
            else
            {
                Crownwood.Magic.Controls.TabPage page = new Crownwood.Magic.Controls.TabPage();
                page.Title = Title;
                page.Control = control;
                tabControlMain.TabPages.Add(page);
                tabControlMain.SelectedTab = page;
            }
        }

        public void AddSinglePage(Control control, string Title)
        {
            if (tabControlMain.Visible == false)
            {
                tabControlMain.Visible = true;
            }
            bool showed = false;
            Crownwood.Magic.Controls.TabPage currPage = null;
            foreach (Crownwood.Magic.Controls.TabPage page in tabControlMain.TabPages)
            {
                if (page.Control.Name == control.Name)
                {
                    showed = true;
                    currPage = page;
                }
            }
            if (!showed)//不存在
            {
                AddNewTabPage(control, Title);
            }
            else
            {
                tabControlMain.SelectedTab = currPage;
            }
        }

        private void processTestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Codematic.test.ProcessForm1 pro_form = new test.ProcessForm1();
            pro_form.Show();

        }
    }
}
